#include <iostream>
#include <string>
#include <SDL.h>
#include <SDL_net.h>

using namespace std;

int main(int argc, char* argv[])
{
	UDPsocket sd;
	IPaddress srvadd;
	UDPpacket* p;
	int quit;
	if (SDLNet_Init() < 0)
	{
		cout << "SDLNet_Init:" << SDLNet_GetError() << endl;
		return -1;
	}
	//打开一个随机的端口
	if (!(sd = SDLNet_UDP_Open(0)))
	{
		cout << "SDLNet_UDP_Open:" << SDLNet_GetError() << endl;
		return -1;
	}
	if (SDLNet_ResolveHost(&srvadd, "127.0.0.1", 15366))
	{
		cout << "SDLNet_ResolveHost:" << SDLNet_GetError() << endl;
		return -1;
	}
	if (!(p = SDLNet_AllocPacket(512)))
	{
		cout << "SDLNet_AllocPacket:" << SDLNet_GetError() << endl;
		return -1;
	}
	quit = 0;
	while (!quit)
	{
		time_t tt = time(nullptr);
		char buffer[100] = { "hello" };
		SDL_strlcat(buffer, to_string(tt).c_str(), 100);
		p->data = (unsigned char*)buffer;
        //memcpy_s(p->data, 512, buffer, 100);
		p->address.host = srvadd.host;//设置主机地址
		p->address.port = srvadd.port; //主机端口
		p->len = strlen((char*)p->data) + 1;
		int len = SDLNet_UDP_Send(sd, -1, p);//发送数据
		//memset(p->data, 0, p->maxlen);
		SDL_Delay(1000);
	}
	SDLNet_FreePacket(p);
	SDLNet_Quit();
	return 0;
}